<template>
	<view class="card">
		<!-- 顶部标题 -->
		<view class="card-header">
			<text :class="video.status === '1' ? 'free' : 'paid'">
				{{ video.status === '1' ? "免费公开课" : "付费课程" }}
			</text>
		</view>

		<!-- 主体内容 -->
		<view class="card-body">
			<!-- 左侧视频封面 -->
			<image class="video-img" src="@/static/video_preview/专业团队.png" />

			<!-- 右侧信息 -->
			<view class="video-info">
				<!-- 视频名称，限制长度 -->
				<text class="video-title">{{ truncate(video.videoName, 10) }}</text>

				<!-- 视频简介 -->
				<text class="video-remake">{{ truncate(video.videoRemake, 6) }}</text>

				<!-- 底部区域：教师名和学习按钮 -->
				<view class="footer">
					<text class="teacher-name">{{ video.teacher.teacherName }}</text>
					<button class="learn-button" @click="startLearning">我要学习</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapMutations
	} from 'vuex';

	export default {
		props: {
			video: {
				type: Object,
				required: true
			}
		},
		methods: {
			...mapMutations(['setVideoAbout']),

			// 截断文本方法
			truncate(text, length) {
				if (!text) {
					return ""; // 如果 text 是 null、undefined 或空字符串，返回空字符串
				}
				return text.length > length ? text.slice(0, length) + "..." : text;
			},

			// 点击"我要学习"按钮的事件处理函数
			startLearning() {
				// 将当前 video 设置为 videoAbout
				this.setVideoAbout(this.video);

				// 跳转到目标页面
				uni.navigateTo({
					url: '/pages/video_display/index'
				});

			}
		}
	};
</script>

<style scoped>
	.card {
		min-width: 200px;
		border-radius: 10px;
		background-color: #f5f8ff;
		box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.1);
		overflow: hidden;
		margin: 10px;
	}

	.card-header {
		background-image: url("@/static/video_preview/blueToWhite.png");
		background-size: cover;
		background-position: center;
		background-repeat: no-repeat;
		padding: 8px;
		font-weight: bold;
		font-size: 14px;
	}

	.free {
		color: #007bff;
	}

	.paid {
		color: #ff4500;
	}

	.card-body {
		display: flex;
		padding: 10px;
	}

	.video-img {
		width: 80px;
		height: 80px;
		border-radius: 8px;
		object-fit: cover;
		margin-right: 10px;
	}

	.video-info {
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		flex: 1;
	}

	.video-title {
		font-size: 16px;
		font-weight: bold;
		color: #333;
	}

	.video-remake {
		font-size: 12px;
		color: #666;
		margin: 5px 0;
		height: 30px;
	}

	.footer {
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.teacher-name {
		font-size: 12px;
		color: #333;
	}

	.learn-button {
		padding: 0px 20px;
		background-color: #007bff;
		color: white;
		font-size: 12px;
		border: none;
		border-radius: 20px;
		cursor: pointer;
		margin-right: 0px;
	}

	.learn-button:hover {
		background-color: #0056b3;
	}
</style>